package scparser.factory.sceneFactory;

import java.awt.Color;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.vecmath.Point3d;

import lib.common.Constants;
import model.Scene;
import model.lights.Light;
import model.lights.PointLight;
import scparser.ParserHelper;
import scparser.factory.lightFactory.LightAbstractFactory;
import scparser.factory.lightFactory.LightFactory;
import scparser.factory.primitiveFactory.FactoryException;


public class PointFactory extends LightFactory implements LightAbstractFactory {
	private Color color;
	private Point3d p;
	private Double power;
	
	public PointFactory(Scene scene) {
		super.scene = scene;
	}

	@Override
	public Light create(List<String> block) throws FactoryException {
		String line = null;
		Iterator<String> it = block.iterator();
		try {
			while (it.hasNext()) {
				line = it.next().trim();

				if (line.equals(Constants.S_CLOSE)) {
					break;
				} else if (line.startsWith("name")) {
					super.name = ParserHelper.takeString(line, "name");
				} else if (line.startsWith("color")) {
					this.color = ParserHelper.getColor(line);
				} else if (line.startsWith("power")) {
					this.power = ParserHelper.takeDouble(line, "power");
				} else if (line.startsWith("p")) {
					this.p = ParserHelper.takePoint(line, "p");
				}
			}
		} catch (IOException e) {
			throw new FactoryException(e.getMessage());
		} catch (FactoryException e) {
			throw new FactoryException(e.getMessage());
		}
		
		PointLight ret = new PointLight(color, power.floatValue(), p);
		if (scene.getLights().contains(ret)) {
			throw new FactoryException("light already exists");
		}
		
		return ret;
	}

}
